using System;
using System.Drawing;
using System.Windows.Forms;
using Spread.Common.DataStore;
using FarPoint.Win.Spread.Model;
using FarPoint.Win.Spread;

namespace Spread.Outline.Features
{
    public partial class GroupBarDemo : Spread.Common.Features.Sample.BaseSampleControl
    {
        private AccessDataSource _dataSource;

        public GroupBarDemo()
        {
            InitializeComponent();
            Name = "Outlook-style Group Bar";

            _dataSource = new DataService().AccessDataSource;

            InitSpread();
            InitControls();
        }

        #region Private methods
        private void InitSpread()
        {
            fpSpread.DataSource = _dataSource.GetNWINDData("Customers").Tables[0];

            fpSpread.AllowColumnMove = true;
            fpSpread.ScrollBarTrackPolicy = ScrollBarTrackPolicy.Both;
            fpSpread.Sheets[0].AllowGroup = true;
            fpSpread.Sheets[0].GroupBarVisible = true;

            for (int col = 0; col < fpSpread.Sheets[0].ColumnCount; col++)
                fpSpread.Sheets[0].Columns[col].Width = fpSpread.Sheets[0].GetPreferredColumnWidth(col, 
                    PreferredSizeColumnOptions.IncludeAll);

            fpSpread.Sheets[0].DefaultStyle.BackColor = Color.AliceBlue;
        }

        private void InitControls()
        {
            numericHeight.Value = fpSpread.ActiveSheet.GroupBarInfo.Height;
            numericHeight.ValueChanged += NumericHeight_ValueChanged;

            btnForeColor.BackColor = fpSpread.ActiveSheet.GroupBarInfo.ForeColor;
            btnForeColor.Click += ShowColorDialog;
            btnForeColor.BackColorChanged += BtnForeColor_BackColorChanged;

            btnBackColor.BackColor = fpSpread.ActiveSheet.GroupBarInfo.BackColor;
            btnBackColor.Click += ShowColorDialog;
            btnBackColor.BackColorChanged += BtnBackColor_BackColorChanged;

            txtGroupText.TextChanged += TxtGroupText_TextChanged;
            btnCreateGroup.Click += BtnCreateGroup_Click;
            btnRemoveLastGroup.Click += BtnRemoveLastGroup_Click;
            radioNotSet.CheckedChanged += RadioNotSet_CheckedChanged;
            radioExpandAll.CheckedChanged += RadioExpandAll_CheckedChanged;
            radioCollapseAll.CheckedChanged += RadioCollapseAll_CheckedChanged;
            radioExpandParents.CheckedChanged += RadioExpandParents_CheckedChanged; 
        }
        #endregion

        #region Event handlers
        private void TxtGroupText_TextChanged(object sender, EventArgs e)
        {
            if (txtGroupText.Text.Length == 0)
                fpSpread.ActiveSheet.GroupBarInfo.ResetText();
            else
                fpSpread.ActiveSheet.GroupBarInfo.Text = txtGroupText.Text;
        }

        #region Button Events
        private void BtnCreateGroup_Click(object sender, EventArgs e)
        {
            if ((fpSpread.Sheets[0].Models.Data != null) && (fpSpread.Sheets[0].Models.Data is GroupDataModel))
            {
                GroupDataModel gm = null;
                gm = fpSpread.Sheets[0].Models.Data as GroupDataModel;
                if (gm.SortInfo.Length < fpSpread.Sheets[0].GroupMaximumLevel)
                {
                    int n = gm.SortInfo.Length;
                    SortInfo[] tmp = new SortInfo[n + 1];
                    int j = 0;
                    int i = 0;
                    while (i < n)
                    {
                        if (gm.SortInfo[i].Index != fpSpread.Sheets[0].GetModelColumnFromViewColumn(fpSpread.Sheets[0].ActiveColumnIndex))
                            tmp[j] = gm.SortInfo[i];
                        else
                            return;
                        tmp[j] = new SortInfo(fpSpread.Sheets[0].GetModelColumnFromViewColumn(fpSpread.Sheets[0].ActiveColumnIndex), true);
                        tmp[j + 1] = gm.SortInfo[i];
                        i = i + 1;
                        j = j + 1;
                    }
                    tmp[j] = new SortInfo(fpSpread.Sheets[0].GetModelColumnFromViewColumn(fpSpread.Sheets[0].ActiveColumnIndex), true);
                    n = n + 1;
                    if (n > 1)
                        gm.Group(tmp, null);
                }
            }
            else
            {
                GroupDataModel gm = new GroupDataModel(fpSpread.Sheets[0].Models.Data);
                SortInfo[] si = new SortInfo[] { new SortInfo(fpSpread.Sheets[0].ActiveColumnIndex, true) };
                gm.Group(si);
                try
                {
                    fpSpread.Sheets[0].Models.Data = gm;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void BtnRemoveLastGroup_Click(object sender, EventArgs e)
        {
            try
            {
                if ((fpSpread.Sheets[0].Models.Data != null) && (fpSpread.Sheets[0].Models.Data is GroupDataModel))
                {
                    GroupDataModel gm = null;
                    gm = fpSpread.Sheets[0].Models.Data as GroupDataModel;

                    int n = gm.SortInfo.Length;
                    SortInfo[] tmp = new SortInfo[n - 1];
                    int i = 0;
                    while (i < n - 1)
                    {
                        tmp[i] = gm.SortInfo[i];
                        i = i + 1;
                    }
                    n = n - 1;
                    if (n >= 1)
                    {
                        gm.Group(tmp, null);
                        return;
                    }
                    else
                        fpSpread.Sheets[0].Models.Data = ((GroupDataModel)fpSpread.Sheets[0].Models.Data).TargetModel;
                }

            }
            catch
            {
            }
        }

        private void BtnForeColor_BackColorChanged(object sender, EventArgs e)
        {
            fpSpread.ActiveSheet.GroupBarInfo.ForeColor = (sender as Button).BackColor;
        }

        private void BtnBackColor_BackColorChanged(object sender, EventArgs e)
        {
            fpSpread.ActiveSheet.GroupBarInfo.BackColor = (sender as Button).BackColor;
        }
        #endregion

        #region RadioButton Events
        private void RadioNotSet_CheckedChanged(object sender, EventArgs e)
        {
            if (radioNotSet.Checked)
                fpSpread.Sheets[0].GroupingPolicy = GroupingPolicy.NotSet;
        }

        private void RadioExpandAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radioExpandAll.Checked)
                fpSpread.Sheets[0].GroupingPolicy = GroupingPolicy.ExpandAll;
        }

        private void RadioCollapseAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radioCollapseAll.Checked == true)
                fpSpread.Sheets[0].GroupingPolicy = GroupingPolicy.CollapseAll;
        }

        private void RadioExpandParents_CheckedChanged(object sender, EventArgs e)
        {
            if (radioExpandParents.Checked == true)
                fpSpread.Sheets[0].GroupingPolicy = GroupingPolicy.ExpandParents;
        }
        #endregion

        private void NumericHeight_ValueChanged(object sender, EventArgs e)
        {
            if (numericHeight.Value > 0)
            {
                fpSpread.ActiveSheet.GroupBarInfo.Height = Convert.ToInt32(numericHeight.Value);
            }
        }

        private void ShowColorDialog(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            dialog.Color = (sender as Button).BackColor;
            if (dialog.ShowDialog() == DialogResult.OK)
                (sender as Button).BackColor = dialog.Color;
        }
        #endregion
    }
}


//
//GroupBarDemo.Designer.cs
//


namespace Spread.Outline.Features
{
    partial class GroupBarDemo
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.btnCreateGroup = new System.Windows.Forms.Button();
            this.btnRemoveLastGroup = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.radioNotSet = new System.Windows.Forms.RadioButton();
            this.radioExpandAll = new System.Windows.Forms.RadioButton();
            this.radioCollapseAll = new System.Windows.Forms.RadioButton();
            this.radioExpandParents = new System.Windows.Forms.RadioButton();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.txtGroupText = new System.Windows.Forms.TextBox();
            this.numericHeight = new System.Windows.Forms.NumericUpDown();
            this.label3 = new System.Windows.Forms.Label();
            this.btnForeColor = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.btnBackColor = new System.Windows.Forms.Button();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericHeight)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox3);
            this.pnlControls.Controls.Add(this.groupBox2);
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White;
            this.fpSpread.StatusBarVisible = true;
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.tableLayoutPanel1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(157, 49);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Groups";
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.btnCreateGroup, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.btnRemoveLastGroup, 1, 0);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(3, 16);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(151, 30);
            this.tableLayoutPanel1.TabIndex = 0;
            // 
            // btnCreateGroup
            // 
            this.btnCreateGroup.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnCreateGroup.Location = new System.Drawing.Point(3, 3);
            this.btnCreateGroup.Name = "btnCreateGroup";
            this.btnCreateGroup.Size = new System.Drawing.Size(69, 24);
            this.btnCreateGroup.TabIndex = 0;
            this.btnCreateGroup.Text = "Create Group";
            this.btnCreateGroup.UseVisualStyleBackColor = true;
            // 
            // btnRemoveLastGroup
            // 
            this.btnRemoveLastGroup.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnRemoveLastGroup.Location = new System.Drawing.Point(78, 3);
            this.btnRemoveLastGroup.Name = "btnRemoveLastGroup";
            this.btnRemoveLastGroup.Size = new System.Drawing.Size(70, 24);
            this.btnRemoveLastGroup.TabIndex = 1;
            this.btnRemoveLastGroup.Text = "Remove Last Group";
            this.btnRemoveLastGroup.UseVisualStyleBackColor = true;
            // 
            // groupBox2
            // 
            this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox2.Controls.Add(this.radioExpandParents);
            this.groupBox2.Controls.Add(this.radioCollapseAll);
            this.groupBox2.Controls.Add(this.radioExpandAll);
            this.groupBox2.Controls.Add(this.radioNotSet);
            this.groupBox2.Location = new System.Drawing.Point(3, 68);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(151, 117);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Grouping Policy";
            // 
            // radioNotSet
            // 
            this.radioNotSet.AutoSize = true;
            this.radioNotSet.Checked = true;
            this.radioNotSet.Location = new System.Drawing.Point(6, 19);
            this.radioNotSet.Name = "radioNotSet";
            this.radioNotSet.Size = new System.Drawing.Size(61, 17);
            this.radioNotSet.TabIndex = 0;
            this.radioNotSet.TabStop = true;
            this.radioNotSet.Text = "Not Set";
            this.radioNotSet.UseVisualStyleBackColor = true;
            // 
            // radioExpandAll
            // 
            this.radioExpandAll.AutoSize = true;
            this.radioExpandAll.Location = new System.Drawing.Point(6, 42);
            this.radioExpandAll.Name = "radioExpandAll";
            this.radioExpandAll.Size = new System.Drawing.Size(75, 17);
            this.radioExpandAll.TabIndex = 1;
            this.radioExpandAll.TabStop = true;
            this.radioExpandAll.Text = "Expand All";
            this.radioExpandAll.UseVisualStyleBackColor = true;
            // 
            // radioCollapseAll
            // 
            this.radioCollapseAll.AutoSize = true;
            this.radioCollapseAll.Location = new System.Drawing.Point(6, 65);
            this.radioCollapseAll.Name = "radioCollapseAll";
            this.radioCollapseAll.Size = new System.Drawing.Size(79, 17);
            this.radioCollapseAll.TabIndex = 2;
            this.radioCollapseAll.TabStop = true;
            this.radioCollapseAll.Text = "Collapse All";
            this.radioCollapseAll.UseVisualStyleBackColor = true;
            // 
            // radioExpandParents
            // 
            this.radioExpandParents.AutoSize = true;
            this.radioExpandParents.Location = new System.Drawing.Point(6, 88);
            this.radioExpandParents.Name = "radioExpandParents";
            this.radioExpandParents.Size = new System.Drawing.Size(100, 17);
            this.radioExpandParents.TabIndex = 3;
            this.radioExpandParents.TabStop = true;
            this.radioExpandParents.Text = "Expand Parents";
            this.radioExpandParents.UseVisualStyleBackColor = true;
            // 
            // groupBox3
            // 
            this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox3.Controls.Add(this.btnBackColor);
            this.groupBox3.Controls.Add(this.label4);
            this.groupBox3.Controls.Add(this.btnForeColor);
            this.groupBox3.Controls.Add(this.label3);
            this.groupBox3.Controls.Add(this.numericHeight);
            this.groupBox3.Controls.Add(this.txtGroupText);
            this.groupBox3.Controls.Add(this.label2);
            this.groupBox3.Controls.Add(this.label1);
            this.groupBox3.Location = new System.Drawing.Point(3, 197);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(151, 154);
            this.groupBox3.TabIndex = 2;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "GroupBar";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(7, 20);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(31, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Text:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(7, 49);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Height:";
            // 
            // txtGroupText
            // 
            this.txtGroupText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtGroupText.Location = new System.Drawing.Point(67, 17);
            this.txtGroupText.Name = "txtGroupText";
            this.txtGroupText.Size = new System.Drawing.Size(81, 20);
            this.txtGroupText.TabIndex = 2;
            // 
            // numericHeight
            // 
            this.numericHeight.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.numericHeight.Location = new System.Drawing.Point(67, 47);
            this.numericHeight.Name = "numericHeight";
            this.numericHeight.Size = new System.Drawing.Size(81, 20);
            this.numericHeight.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(7, 87);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(55, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "ForeColor:";
            // 
            // btnForeColor
            // 
            this.btnForeColor.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnForeColor.Location = new System.Drawing.Point(67, 82);
            this.btnForeColor.Name = "btnForeColor";
            this.btnForeColor.Size = new System.Drawing.Size(81, 23);
            this.btnForeColor.TabIndex = 5;
            this.btnForeColor.UseVisualStyleBackColor = true;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(7, 118);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(59, 13);
            this.label4.TabIndex = 6;
            this.label4.Text = "BackColor:";
            // 
            // btnBackColor
            // 
            this.btnBackColor.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnBackColor.Location = new System.Drawing.Point(67, 113);
            this.btnBackColor.Name = "btnBackColor";
            this.btnBackColor.Size = new System.Drawing.Size(81, 23);
            this.btnBackColor.TabIndex = 7;
            this.btnBackColor.UseVisualStyleBackColor = true;
            // 
            // GroupBarDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "GroupBarDemo";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericHeight)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        private System.Windows.Forms.Button btnCreateGroup;
        private System.Windows.Forms.Button btnRemoveLastGroup;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Button btnBackColor;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btnForeColor;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.NumericUpDown numericHeight;
        private System.Windows.Forms.TextBox txtGroupText;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.RadioButton radioExpandParents;
        private System.Windows.Forms.RadioButton radioCollapseAll;
        private System.Windows.Forms.RadioButton radioExpandAll;
        private System.Windows.Forms.RadioButton radioNotSet;
    }
}
